<template>
  <div class="app-container">
    <!--    0人工审核中，1审核通过，-1不通过，2未发布，3机器审核中-->
    <el-input style="width: 150px; padding: 10px" v-model="keyword" placeholder="请输入内容"></el-input>
    <div class="block">
      <el-date-picker
        v-model="filterTime.date"
        type="datetimerange"
        value-format="yyyy-MM-dd"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions"
        @change="dateFormat"
      />
    </div>
    <el-button type="primary" round @click="handleSearch">搜索</el-button>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="房间编号" width="110">
        <template slot-scope="scope">
          {{ scope.row.number }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="房主id" width="110">
        <template slot-scope="scope">
          {{ scope.row.creatorUsername }}
        </template>
      </el-table-column>
      <el-table-column label="房间标题" width="150">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="类型" width="50">
        <template slot-scope="scope">
          {{ scope.row.typeStr }}
        </template>
      </el-table-column>

      <el-table-column label="总流水" width="150">
        <template slot-scope="scope">
          {{ scope.row.totalBillNumber }}
        </template>
      </el-table-column>

      <el-table-column label="周流水" width="100">
        <template slot-scope="scope">
          {{ scope.row.weekBillNumber }}
        </template>
      </el-table-column>

      <el-table-column label="日流水" width="100">
        <template slot-scope="scope">
          {{ scope.row.todayBillNumber }}
        </template>
      </el-table-column>

      <el-table-column label="昨日流水" width="100">
        <template slot-scope="scope">
          {{ scope.row.yesterdayBillNumber }}
        </template>
      </el-table-column>

      <el-table-column label="封面图" width="200" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.imgUrl"
            :preview-src-list="[scope.row.imgUrl]"
          />
        </template>
      </el-table-column>
      <el-table-column label="背景图" width="200" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.bgUrl"
            :preview-src-list="[scope.row.bgUrl]"
          />
        </template>
      </el-table-column>
      <el-table-column label="公告" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.notice&&scope.row.notice.length>100? scope.row.notice.slice(0, 50)+"..." :  scope.row.notice }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" round @click="dialogFormVisible = true; updatePartyDTO.id=scope.row.id">
            修改房间
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改房间" :visible.sync="dialogFormVisible">
      房间状态：
      <el-radio v-model="updatePartyDTO.isOpen" label="true">开启</el-radio>
      <el-radio v-model="updatePartyDTO.isOpen" label="false">关闭</el-radio>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateParty">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :current-page="page"
      :total="total"
      @current-change="fetchData">
    </el-pagination>
  </div>
</template>

<script>
import { getList, deleteById, realDeleteById,updateParty } from '@/api/party'
import {getUser} from "@/utils/auth";

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      list: null,
      listLoading: true,
      updatePartyDTO: {},
      status: '-10',
      filterTime: {
        startTime: '',
        endTime: '',
        date: ''
      },
      page: 1,
      size: 10,
      total: 0,
      dialogFormVisible: false,
      keyword: null
    }
  },
  created() {
    this.fetchData(this.page)
  },
  methods: {
    handleClick(tab, event) {
      this.fetchData(this.page)
    },
    handleUpdateParty() {
      this.dialogFormVisible = false;
      updateParty(this.updatePartyDTO).then(res =>{
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
      })
      this.fetchData(this.page)
    },
    handleSearch() {
      this.listLoading = true
      const user = getUser();
      const json = JSON.parse(user);
      getList({ page: this.page, size: this.size, phone: json.phone, keyword: this.keyword,startTime: this.filterTime.startTime, endTime: this.filterTime.endTime  }).then(response => {
        this.total = response.total
        console.log(this.page)
        this.list = response.data
        this.listLoading = false
      })
    },
    dateFormat(picker) {
      this.filterTime.startTime = picker[0]
      this.filterTime.endTime = picker[1]
    },
    fetchData(val) {
      this.page = val
      this.listLoading = true
      const user = getUser();
      const json = JSON.parse(user);
      getList({ page: this.page, size: this.size, phone: json.phone }).then(response => {
        this.total = response.total
        console.log(this.page)
        this.list = response.data
        this.listLoading = false
      })
    },
    handelDelete(id) {
      this.$confirm('此操作将删除该动态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handelRealDelete(id) {
      this.$confirm('此操作将彻底删除该动态,请谨慎操作！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        realDeleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
